<?php

class UsersController extends AppController {

	public $uses = array("User");
	public $components = array('Paginator');
	public $helpers = array('Paginator');
	public $paginate = array(
			'limit' => 5,
			'order' => array(
					'User.username' => 'asc'
			)
	);
	
	public function index() {
		$this->Paginator->settings = $this->paginate;
		$users = $this->Paginator->paginate('User');
		$this->set('users', $users);
		$this->set('loggedUserId', $this->getLoggedUserId());
		$this->set('isLoggedUserAdmin', $this->isLoggedUserAdmin());
	}

	public function add() {
        $this->layout="ajax";

		if ($this->request->is('post')) {
			$this->User->create();
            $data = $this->request->data;

            $password = $data['password'];
            $password2 = $data['password2'];

            $formData = array();
            if ($this->checkPasswords($password, $password2)) {
                $formData[$this->User->alias]['password'] = Security::hash($password, null, true);
            } else {
                return;
            }
            $formData[$this->User->alias]['username'] = $data['username'];
            $formData[$this->User->alias]['role'] = $data['role'];

            $this->saveUser($formData);
        }
    }

	public function edit() {
        $this->view = "add";
        $this->layout = "ajax";

        if ($this->request->is('post')) {
            $data = $this->request->data;

            $password = $data['password'];
            $password2 = $data['password2'];
            $formData = array();

            if ($this->checkPasswords($password, $password2)) {
                $formData[$this->User->alias]['password'] = Security::hash($password, null, true);
            } else {
                return;
            }
            $formData[$this->User->alias]['username'] = $data['username'];
            $formData[$this->User->alias]['role'] = $data['role'];

            $this->User->id = $data['id'];
            $this->saveUser($formData);

        } else {
            $id = $this->request->query['userId'];
            $this->User->id = $id;
            if (!$this->User->exists()) {
                throw new NotFoundException(__('Brak takiego użytkownika'), 'msg');
            }
            $this->set('user', $this->User->findById($id));
        }
    }

	public function delete() {
        $this->layout = "ajax";
        $this->autoRender = false;

		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $this->request->data('id');
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Brak takiego użytkownika'), 'msg');
		}
		if ($this->User->delete()) {
			$this->Session->setFlash(__('Użytkownik został usunięty.'), 'msg');
		}
	}
	
	public function activate() {
		$this->layout = "ajax";
		$this->autoRender = false;		
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $this->request->data('id');
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Brak takiego użytkownika'), 'msg');
		}
		$this->User->set("activated", true);
		$this->User->save();
		
		$this->Session->setFlash(__('Użytkownik został aktywowany.'), 'msg');
			
	}
	
	public function deactivate() {
		$this->layout = "ajax";
		$this->autoRender = false;
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $this->request->data('id');
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Brak takiego użytkownika'), 'msg');
		}
		$this->User->set("activated", false);
		$this->User->save();
	
		$this->Session->setFlash(__('Użytkownik został deaktywowany.'), 'msg');
			
	}

    public function setActive() {
        $loggedUser = $this->User->findById($this->getLoggedUserId());
        if ($loggedUser->role == 1) {
            // admin - aktywacja


        } else {
            $this->Session->setFlash(__('Brak uprawnień do aktywowania nowych użytkowników.'), 'warning');
        }
    }

    private function saveUser($userData) {
        if ($this->User->save($userData)) {
            $this->Session->setFlash(__('Użytkownik został zapisany.'), 'msg');
        } else {
            foreach ($this->User->validationErrors as $key => $value) {
                $this->Session->setFlash(__($value[0]), 'warning');
            }
        }
        $this->autoRender = false;
    }

    private function checkPasswords($pass, $repeatPass) {
        if (strlen($pass) >= 4) {
            if (strcasecmp($pass, $repeatPass) != 0) {
                $this->Session->setFlash(__('Podane hasła są różne.'), 'warning');
                return false;
            }
        } else {
            return false;
        }
        return true;
    }
}
